package com.gzpi.hwgggh.service.impl;

import com.gzpi.hwgggh.entity.Dictionary;
import com.gzpi.hwgggh.mapper.*;
import com.gzpi.hwgggh.service.StatisticsService;
import com.gzpi.hwgggh.sysenum.EnumErrorCode;
import com.gzpi.hwgggh.utils.CommonTools;
import com.gzpi.hwgggh.utils.Result;
import com.gzpi.hwgggh.vo.ClickTableVo;
import com.gzpi.hwgggh.vo.StatisticsGraphVo;
import com.gzpi.hwgggh.vo.StatisticsTableVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @date : 2021-01-07 16:35
 **/
@Service
@Transactional
public class StatisticsServiceImpl implements StatisticsService {

    @Autowired
    private DictionaryMapper dictionaryMapper;

    @Autowired
    private DWXMapper dwxMapper;

    @Autowired
    private DWCMapper dwcMapper;

    @Autowired
    private DWGMapper dwgMapper;

    @Autowired
    private GGPMapper ggpMapper;

    @Autowired
    private DWFJMapper dwfjMapper;

    @Override
    public Result selectDWstatisticsTable(StatisticsTableVo statisticsTableVo) {
        Map<String,Object> map = new HashMap<>();
        List<Map> list = new ArrayList<>();
        int total = 0;
        if (statisticsTableVo.getDwtype().equals("X")){
            list = dwxMapper.selectDWXTable(statisticsTableVo);
//            total = dwxMapper.selectDWXTableTotal(statisticsTableVo);
        }else if (statisticsTableVo.getDwtype().equals("G")){
            list = dwgMapper.selectDWGTable(statisticsTableVo);
//            total = dwgMapper.selectDWGTableTotal(statisticsTableVo);
        }else if (statisticsTableVo.getDwtype().equals("C")){
            list = dwcMapper.selectDWCTable(statisticsTableVo);
//            total = dwcMapper.selectDWCTableTotal(statisticsTableVo);
        }

        List<Map<String, Object>> list1 = new ArrayList<>();
        if(list!=null && list.size()>0) {
            list1 = CommonTools.mergeInformation(list);
        }
        for (Map mapl : list1) {
            String qsmc = (String) mapl.get("QSMC");
            if (StringUtils.isNotBlank(qsmc)) {
                String jzqs = (String) mapl.get("JZQS");
                int i = jzqs.indexOf("（");
                jzqs = jzqs.substring(0, i>-1?i:jzqs.length());
                mapl.put("JZQS", jzqs + "（" + qsmc + "）");
            }
        }

        int ggpnum = 0 ;
        for (Map<String, Object> map2 : list1) {
            Object count = map2.get("COUNT") != null ? map2 .get("COUNT") : "0";
            ggpnum += Integer.valueOf(count.toString());
        }

        map.put("rows",list1);
        map.put("ggpnum",ggpnum);
        /*map.put("total",list1.size());
        if (statisticsTableVo.getLimit()!=null && statisticsTableVo.getLimit()!=0 && statisticsTableVo.getPageSize()!=null && statisticsTableVo.getPageSize()!=0){
            List<List<Map<String,Object>>> pageList = CommonTools.createList(list1,statisticsTableVo.getPageSize());
            map.put("rows",pageList.get(statisticsTableVo.getLimit()-1));
        }*/
        if (list.size()>0){
            return  Result.createSuccess(EnumErrorCode.SUCCESS_MESSAGE.toString("统计数据查询成功"),map);
        }
        return Result.createError(EnumErrorCode.ERROR_MESSAGE.toString("统计数据查询失败"),map);
    }

    @Override
    public Result GroupCountGGP(StatisticsGraphVo graphVo) {
        if (graphVo!=null) {
            if ("DW_X".equals(graphVo.getDwtable())) {
                graphVo.setGgptable("GGP_X");
            } else {
                if (!"ghcs".equals(graphVo.getTj())) {
                    if ("DW_C".equals(graphVo.getDwtable())) {
                        graphVo.setGgptable("GGP_C");
                    } else if ("DW_G".equals(graphVo.getDwtable())) {
                        graphVo.setGgptable("GGP_G");
                    }
                }else if ("DW_G".equals(graphVo.getDwtable())){
                    graphVo.setGgptable("GGP_G");
                }
            }
            List<Map> list = null;
            if ("ghcs".equals(graphVo.getTj())) {
                list = ggpMapper.GroupGhcsUnionAll(graphVo);
            }else{
                list = ggpMapper.GroupCountGGP(graphVo);
            }
            return Result.createSuccess(EnumErrorCode.SUCCESS_MESSAGE.toString("广告牌分组查询成功"), list);
        }
        return Result.createError(EnumErrorCode.ERROR_MESSAGE.toString("广告牌分组查询失败"));
    }

    @Override
    public Result GroupCountDW(StatisticsGraphVo graphVo) {
        List<Map> list = ggpMapper.GroupCountDW(graphVo);
        if (list.size()>0){
            return Result.createSuccess(EnumErrorCode.SUCCESS_MESSAGE.toString("点位分组查询成功"),list);
        }
        return Result.createError(EnumErrorCode.ERROR_MESSAGE.toString("点位分组查询失败"));
    }

    @Override
    public Result getCCSum(StatisticsGraphVo graphVo) {
        try {
            if("DW_X".equals(graphVo.getDwtable())){
                graphVo.setGgptable("GGP_X");
            }else{
                if ("DW_C".equals(graphVo.getDwtable())){
                    graphVo.setGgptable("GGP_C");
                    graphVo.setDWTYPE("拆除点");
                }else if ("DW_G".equals(graphVo.getDwtable())){
                    graphVo.setGgptable("GGP_G");
                    graphVo.setDWTYPE("规划点");
                }
            }
            List<String> ggccs = ggpMapper.selectGGCCByVo(graphVo);
            if (ggccs != null) {
                Double sum = 0D;
                for (String ggcc : ggccs) {
                    if (StringUtils.isNotBlank(ggcc) && ggcc.lastIndexOf("=")>-1) {
                        int start = ggcc.lastIndexOf("=");
                        int end = ggcc.length();
                        if (ggcc.lastIndexOf("㎡") > start) {
                            end = ggcc.lastIndexOf("㎡");
                        } else if (ggcc.lastIndexOf("平方米") > start) {
                            end = ggcc.lastIndexOf("平方米");
                        } else if (ggcc.lastIndexOf("m2") > start) {
                            end = ggcc.lastIndexOf("m2");
                        } else if (ggcc.lastIndexOf("M2") > start) {
                            end = ggcc.lastIndexOf("M2");
                        } else if (ggcc.lastIndexOf("m") > start) {
                            end = ggcc.lastIndexOf("m");
                        } else if (ggcc.lastIndexOf("M") > start) {
                            end = ggcc.lastIndexOf("M");
                        }
                        String m =ggcc.substring(start + 1, end);
                        if ("NaN".equalsIgnoreCase(m)) continue;
                        m = m.trim().replace(" ",".");
                        sum += Double.valueOf(m);
                    }
                }
                return Result.createSuccess(EnumErrorCode.SUCCESS_MESSAGE.toString("成功"),String.format("%.3f",sum));
            }
            return Result.createError(EnumErrorCode.ERROR_OBJECT_SEARCH.toString());
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return Result.createError(EnumErrorCode.ERROR_MESSAGE.toString("服务器异常"));
        }
    }

    @Override
    public Result getJGSum(StatisticsGraphVo graphVo) {
        try {
            List<String> list = dwfjMapper.selectGGPJGByVo(graphVo);
            if (list.size()>0){
                double sum = 0;
                for (String jg : list) {
                    if (StringUtils.isNotBlank(jg)) {
                        sum += Double.valueOf(jg);
                    }
                }
                sum = sum/10000;
//                return Result.createSuccess(EnumErrorCode.SUCCESS_MESSAGE.toString("成功"),String.format("%.2f",sum));
                return Result.createSuccess(EnumErrorCode.SUCCESS_MESSAGE.toString("成功"),String.valueOf(sum));
            }
            return Result.createError(EnumErrorCode.ERROR_OBJECT_SEARCH.toString("无数据"));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.createError(EnumErrorCode.ERROR_SYSTEM_UNKNOWN.toString());
        }
    }

    @Override
    public Result clickTable(ClickTableVo clickTableVo) {
        if (StringUtils.isBlank(clickTableVo.getTjKey()) || StringUtils.isBlank(clickTableVo.getTjValue())){
            return Result.createError(EnumErrorCode.ERROR_MESSAGE.toString("参数不能为空"));
        }
        List<Map> list = new ArrayList<>();
        if (clickTableVo.getDwType().equals("X")){
            list = dwxMapper.selectDWXTableByClickTableVo(clickTableVo);
//            total = dwxMapper.selectDWXTableTotal(statisticsTableVo);
        }else if (clickTableVo.getDwType().equals("G")){
            if ("ghcs".equals(clickTableVo.getTjKey()) && "拆除".equals(clickTableVo.getTjValue())){
                list = dwcMapper.selectDWCTableByClickTableVo(clickTableVo);
            }else {
                list = dwgMapper.selectDWGTableByClickTableVo(clickTableVo);
            }
//            total = dwgMapper.selectDWGTableTotal(statisticsTableVo);
        }else if (clickTableVo.getDwType().equals("C")){
            list = dwcMapper.selectDWCTableByClickTableVo(clickTableVo);
//            total = dwcMapper.selectDWCTableTotal(statisticsTableVo);
        }

        List<Map<String, Object>> list1 = new ArrayList<>();
        if(list!=null && list.size()>0) {
            list1 = CommonTools.mergeInformation(list);
        }

        for (Map mapl : list1) {
            String qsmc = (String) mapl.get("QSMC");
            if (StringUtils.isNotBlank(qsmc)) {
                String jzqs = (String) mapl.get("JZQS");
                int i = jzqs.indexOf("（");
                jzqs = jzqs.substring(0, i>-1?i:jzqs.length());
                mapl.put("JZQS", jzqs + "（" + qsmc + "）");
            }
        }

        if (list.size()>0){
            return  Result.createSuccess(EnumErrorCode.SUCCESS_MESSAGE.toString("统计数据查询成功"),list1);
        }
        return Result.createError(EnumErrorCode.ERROR_MESSAGE.toString("统计数据查询失败"));
    }
}
